package com.thewaybetter.modules.client.composites;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.thewaybetter.modules.client.model.SlimIdea;
import com.thewaybetter.modules.client.services.IdeaService;
import com.thewaybetter.modules.client.services.IdeaServiceAsync;

public class MessagePanel extends Composite {

	final VerticalPanel vp = new VerticalPanel();
	final VerticalPanel mp = new VerticalPanel();
	
	List<IdeaPanel> ideas = new ArrayList<IdeaPanel>();
	
	public MessagePanel(){
		vp.setWidth("500");
		vp.setStylePrimaryName("message-Panel");
		vp.add(mp);
		createBottomBar();
		initWidget(vp);
	}
	
	public void addNewIdea(IdeaPanel panel){
		ideas.add(panel);
		mp.add(panel);
	}
	
	public void createBottomBar(){
		final VerticalPanel container = new VerticalPanel();
		final VerticalPanel vpForm = new VerticalPanel();
		final HorizontalPanel hp = new HorizontalPanel();
		final TextBox title = new TextBox();
		final TextArea content = new TextArea();
		Button btn = new Button("Create idea");
		
		container.setHeight("220");
		container.setStyleName("idea-Panel");
		title.setWidth("500");
		content.setSize("400", "130");
		
		btn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				final IdeaServiceAsync service = GWT.create(IdeaService.class);
				SlimIdea i = new SlimIdea();
				i.setTitle(title.getText());
				i.setContent(content.getText());
				service.save(i, new AsyncCallback<SlimIdea>(){
					public void onFailure(Throwable caught) {
						
					}
					public void onSuccess(SlimIdea result) {
						title.setText("");
						content.setText("");
					}
				});
			}
		});
		
		container.add(vpForm);
		vpForm.add(new HTML("<b>Add new Idea</b><br/><hr/>"));
		vpForm.add(new Label("Title:"));
		vpForm.add(title);
		vpForm.add(new Label("Content:"));
		vpForm.add(content);
		vpForm.add(new HTML("<br/>"));
		vpForm.add(btn);
		//container.add(hp);
		vp.add(container);
	}
	
	public void clearMessages(){
		mp.clear();
		ideas.clear();
	}
	
}
